package com.honry.common.infrastructure.persistence;

import org.hibernate.Session;
import org.hibernate.SessionFactory;


import com.honry.common.spring.ApplicationContextProvider;

/**
 * 持久化hibernateSession提供者
 * @author machao
 *
 */
public class PersistenceManagerProvider {
	private Session hibernateSession;
	/**
	 * 根据beanName从Spring容器中获取持久化提供者。
	 * @param aHibernateSessionFactoryName
	 * @return
	 */
	public static PersistenceManagerProvider forHibernateSessionNamed(
            String aHibernateSessionFactoryName) {

        SessionFactory sessionFactory = (SessionFactory)
                ApplicationContextProvider
                    .instance()
                    .applicationContext()
                    .getBean(aHibernateSessionFactoryName);

        return new PersistenceManagerProvider(sessionFactory.openSession());
    }
	public PersistenceManagerProvider() {
		super();
	}
	public PersistenceManagerProvider(Session hibernateSession) {
		super();
		this.setHibernateSession(hibernateSession);
	}
	
	/**
	 * hibernateSession
	 * @return
	 */
	public Session hibernateSession() {
		return hibernateSession;
	}
	
	/**
	 * hibernateSession是否存在
	 * @return
	 */
	public boolean hasHibernateSession(){
		return this.hibernateSession()!=null;
	}
	private void setHibernateSession(Session hibernateSession) {
		this.hibernateSession = hibernateSession;
	}
}
